package app.fugema.shop.main.goods.view;

import java.util.List;

import com.base.common.model.http.jackSon.JacksonUtils;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.ada.GeneralRecyclerAdapter;
import com.base.common.view.adapter.connector.BaseItemMultiType;
import com.base.common.view.adapter.connector.BaseUniversalItemMultiType;
import com.base.common.view.base.BaseDialogFragment;
import com.jeremyliao.liveeventbus.LiveEventBus;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import app.fugema.shop.R;
import app.fugema.shop.databinding.FragmentShopCouponDialogBinding;
import app.fugema.shop.main.goods.adapter.ShopCouponItemMultiType;
import app.fugema.shop.main.goods.model.FreeFreightFootBean;
import app.fugema.shop.main.goods.model.FullDiscountBean;
import app.fugema.shop.main.goods.model.FullDiscountFootBean;
import app.fugema.shop.main.goods.model.ShopCouponBean;
import app.fugema.shop.main.goods.vm.GoodsVM;

public class ShopCouponDialogFragment extends BaseDialogFragment<FragmentShopCouponDialogBinding, GoodsVM>
{

    public static ShopCouponDialogFragment getInstance(List<ShopCouponBean> couponList , int freeFreight , FullDiscountBean fullDiscount)
    {
        ShopCouponDialogFragment shoppingCartBottomFragment = new ShopCouponDialogFragment();
        Bundle args = new Bundle();
        args.putString("couponList", JacksonUtils.transBean2Json( couponList ));
        args.putInt("freeFreight", freeFreight);
        if( UIUtils.isNotEmpty( fullDiscount ) )
        {
            args.putString("fullDiscount", JacksonUtils.transBean2Json( fullDiscount ));
        }
        shoppingCartBottomFragment.setArguments(args);
        return shoppingCartBottomFragment;
    }

    @Override
    protected FragmentShopCouponDialogBinding initDataBinding(LayoutInflater inflater, ViewGroup container)
    {
        return DataBindingUtil.inflate(inflater, R.layout.fragment_shop_coupon_dialog, container, false);
    }

    private List<ShopCouponBean> mCouponList;

    private GeneralRecyclerAdapter couponAdapter;
    private int mFreeFreight;
    private FullDiscountBean mFullBean;
    @Override
    public void initView()
    {
        super.initView();

        String couponStr = getArguments().getString("couponList","");
        mFreeFreight = getArguments().getInt("freeFreight",0);
        if( UIUtils.isNotEmpty( couponStr ) )
        {
            mCouponList = JacksonUtils.getConverList( couponStr,ShopCouponBean.class );
        }

        String discountStr = getArguments().getString("fullDiscount","");
        if( UIUtils.isNotEmpty( discountStr ) )
        {
            mFullBean = JacksonUtils.getJsonBean( discountStr,FullDiscountBean.class );
        }
        binding.ivBack.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                dismissAllowingStateLoss();
            }
        });


        couponAdapter = new GeneralRecyclerAdapter()
        {
            @Override
            public void initMultiItemType()
            {
                putMultiItemType( new ShopCouponItemMultiType(ShopCouponDialogFragment.this , viewModel ));

                putMultiItemType(new BaseUniversalItemMultiType<FreeFreightFootBean>(R.layout.footer_freefight_view)
                {
                    @Override
                    public int getItemViewType()
                    {
                        return TYPE_FOOT;
                    }
                });

                putMultiItemType(new BaseUniversalItemMultiType<FullDiscountFootBean>(R.layout.footer_fulldiscount_view)
                {
                    @Override
                    public int getItemViewType()
                    {
                        return TYPE_FOOT_ONE;
                    }
                });
            }
        };
        binding.rvRecyclerView.setAdapter(couponAdapter);

        if( UIUtils.isNotEmpty( mCouponList ) )
        {
            couponAdapter.setDataList( mCouponList );
        }
        if( mFreeFreight > 0 )
        {
            FreeFreightFootBean footBean = new FreeFreightFootBean();
            footBean.setItemType(BaseItemMultiType.TYPE_FOOT);
            footBean.setFreeFreight( mFreeFreight );
            couponAdapter.addFooterView(footBean);
        }
        if( UIUtils.isNotEmpty( mFullBean ) && mFullBean.getFullDiscountActiveStatus() == 1 )
        {
            //有满减信息 并且 活动在进行
            FullDiscountFootBean discountFootBean = new FullDiscountFootBean();
            discountFootBean.setItemType(BaseItemMultiType.TYPE_FOOT_ONE);
            if( UIUtils.isNotEmpty( mFullBean.getRules() ) && !mFullBean.getRules().isEmpty()  )
            {
                StringBuffer sbr = new StringBuffer();
                for (FullDiscountBean.RulesBean rule : mFullBean.getRules())
                {
                    sbr.append("满").append(rule.getQuota()).append("减").append(rule.getDiscount()).append("元；");
                }
                discountFootBean.setFullDiscount(sbr.toString());
                couponAdapter.addFooterView( discountFootBean );
            }
        }
        //领取成功直接 关闭 弹框
        LiveEventBus.get("applySuccess",Integer.class)
                    .observe(this, result -> {

                        dismissAllowingStateLoss();

                    } );

    }


}
